package com.zzj.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Description TODO
 * @Author zzj
 * @Date 2020/8/6 19:06
 */
@WebServlet("/user/*")
public class UserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 测试 ：HttpServletRequest     HttpSession    ServletContext 的作用域
        // 获取session
        HttpSession session = req.getSession();
        // 获取application
        ServletContext application = req.getServletContext();
        // 分别保存数据
        req.setAttribute("user", "HttpServletRequest");
        session.setAttribute("user", "HttpSession");
        application.setAttribute("user", "ServletContext");

        // 视图渲染
        req.getRequestDispatcher("/main.jsp").forward(req, resp);
    }
}
